﻿using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.Serialization;
using CloudShot.Core.Types;

namespace CloudShot.Settings
{
	public sealed class ImageStorageSettings : IXmlSerializable
	{
		public ImageStorageSettings()
		{
			StorageSettings = new List<ImageStorageSettingsItem>();
		}

		#region IXmlSerializable

		public XmlSchema GetSchema()
		{
			return null;
		}

		public void ReadXml(XmlReader reader)
		{
			var root = XNode.ReadFrom(reader) as XElement;
			if (root == null)
				return;

			Name = root.Attribute("Name").Value;

			foreach (XElement node in root.Nodes().OfType<XElement>())
			{
				StorageSettings.Add(new ImageStorageSettingsItem
				{
					Name = node.Name.LocalName,
					Value = node.Value
				});
			}
		}

		public void WriteXml(XmlWriter writer)
		{
			writer.WriteAttributeString("Name", Name);

			foreach (ImageStorageSettingsItem setting in StorageSettings)
			{
				writer.WriteElementString(setting.Name, setting.Value.ToString());
			}
		}

		#endregion

		public string Name { get; set; }

		public ImageSaveFormat CurrentImageFormat { get; set; }

		public List<ImageStorageSettingsItem> StorageSettings { get; set; }
	}

	public sealed class ImageStorageSettingsItem
	{
		public string Name { get; set; }
		public object Value { get; set; }
	}
}